<?php 
	$strDir = "/tmp/sql/";
	if(!file_exists($strDir))
	{
		if(!mkdir($strDir))
		{
			echo "failed to create ".$strDir;
			return false;
		}
	}
	
	$strDatabase = "dbIngersoll";
?>

<div style="text-align: left;">
	<pre><?
		/* set-up multidimensional array
			 * $arrTable = array
				(
					"tblProduct"=>array
					(
						"ID"=>array
						(
							Type=>"255.255.255.0",
							NULL=>false,
							key=>
						)
						
					)
				);
			 */
			//$_arrRouter[1]['sb_lan_netmask']['UPDATED'] = true;
			//$_arrRouter[1]['sb_lan_netmask']['value'] = "255.255.248.0";
		 $arrTable = array( "" => array ( "" => array ( "" => "" )	));
	
		$strSql =  "SHOW TABLES 
					FROM ".$strDatabase;
		$qryTable = new MySqlResults($strSql);
		//print_array($qryTables->myrow);
		//echo $qryTables->myrow[0];
		//print_object($qryTables);
		$strPrimaryKey = null;
		do{
			
			if($qryTable->myrow[0] != "")
			{
				$strFullFileName = $strDir.$qryTable->myrow[0].".class.php";
				
				if(file_exists($strFullFileName))
					unlink($strFullFileName);
					
				$filDbClass = fopen($strFullFileName, "x");
				
				$strDatabaseClass = "<?php";
				$strDatabaseClass .= "\n/* ".$qryTable->myrow[0]." */";
				$strClassName = str_ireplace(" ","",ucwords(str_ireplace("_"," ",$qryTable->myrow[0])));
				//$strDatabaseClass .= "\n\tcreate ".$qryTable->myrow[0].".class.php";
				$strDatabaseClass .= "\nclass ".$strClassName." extends NocForm";
				$strDatabaseClass .= "\n\t{\n";
				
				$strFieldSql = "SHOW COLUMNS 
								FROM ".$qryTable->myrow[0];
				
	
				$qryField = new MySqlResults($strFieldSql);
				$iRecordCount = 0;
				$strAutoInc = null;
				/* define variables */
				do{
					if($qryField->myrow[0] != "")
					{
						$strDatabaseClass .= "\t\tprotected \$_".$qryField->myrow[0]."                 = array();\n";
						
						$arrTable[$qryTable->myrow[0]][$qryField->myrow[0]]['Type'] = $qryField->myrow[1];//'Type'
						$arrTable[$qryTable->myrow[0]][$qryField->myrow[0]]['Null'] = $qryField->myrow[2];//'Null'
						$arrTable[$qryTable->myrow[0]][$qryField->myrow[0]]['Key'] = $qryField->myrow[3];//'Key'
						$arrTable[$qryTable->myrow[0]][$qryField->myrow[0]]['Default'] = $qryField->myrow[4];//Default
						$arrTable[$qryTable->myrow[0]][$qryField->myrow[0]]['Extra'] = $qryField->myrow[5];//'Extra'
						
						if($qryField->myrow[3] == "PRI")
							$strPrimaryKey = $qryField->myrow[0];

						if(trim($qryField->myrow[5]) == "auto_increment")
							$strAutoInc = $qryField->myrow[0];
							
					}
	
				}while($qryField->myrow = mysql_fetch_array($qryField->result));
				/* write construct */
				$strDatabaseClass .= "\n";
				$strDatabaseClass .= "\t\tfunction __construct()\n";
				$strDatabaseClass .= "\t\t{\n";
				$strDatabaseClass .= "\t\t\tparent::__construct();\n";
				$strDatabaseClass .= "\t\t\t\$this->getRow();\n";
				$strDatabaseClass .= "\t\t}\n";
				
				/* reset row pointer */
				mysql_data_seek($qryField->result, 0);
				/* write getters */
				do{
					if($qryField->myrow[0] != "")
					{
						$strDatabaseClass .= "\t\tpublic function get_".$qryField->myrow[0]."()\n";
						$strDatabaseClass .= "\t\t{\n";
						$strDatabaseClass .= "\t\t\t\treturn  \$this->_get_".$qryField->myrow[0]."Value();\n";
						$strDatabaseClass .= "\t\t}\n\n";
						
						$strDatabaseClass .= "\t\tprivate function _get_".$qryField->myrow[0]."Value()\n";
						$strDatabaseClass .= "\t\t{\n";
						$strDatabaseClass .= "\t\t\tif(isset(\$this->_".$qryField->myrow[0]."['value']))\n";
						$strDatabaseClass .= "\t\t\t\treturn \$this->_".$qryField->myrow[0]."['value'];\n";
						$strDatabaseClass .= "\t\t\telse\n";
						$strDatabaseClass .= "\t\t\t\treturn false;\n";
						$strDatabaseClass .= "\t\t}\n\n";
					}
	
				}while($qryField->myrow = mysql_fetch_array($qryField->result));
				
				
				/* reset row pointer */
				mysql_data_seek($qryField->result, 0);
				/* write setters */
				do{
					if($qryField->myrow[0] != "")
					{
						$strDatabaseClass .= "\n\t\tpublic function set_".$qryField->myrow[0]."Value(\$str".$qryField->myrow[0].")\n";
						$strDatabaseClass .= "\t\t{\n";
						$strDatabaseClass .= "\t\t\tif(\$this->validateInput(\"validatexxxxx\", \$str".$qryField->myrow[0].", \"".$qryField->myrow[0]." \"))\n";
						$strDatabaseClass .= "\t\t\t\t\treturn(\$this->_setNewValue(\$this->_".$qryField->myrow[0].", \$str".$qryField->myrow[0]."));\n";
						$strDatabaseClass .= "\t\t\telse\n";
						$strDatabaseClass .= "\t\t\t\t\treturn false;\n";	
						$strDatabaseClass .= "\t\t}\n";
					}
	
				}while($qryField->myrow = mysql_fetch_array($qryField->result));
				
				
				
				/* write update */
				
				$strDatabaseClass .= "\t\tpublic function update{$strClassName}()\n";
				$strDatabaseClass .= "\t\t{\n";
				$strDatabaseClass .= "\t\t\tif(\$this->_boFailedValidation)\n";
				$strDatabaseClass .= "\t\t\t\t\treturn false;\n";
				$strDatabaseClass .= "\t\t\t\t\t\$strErrorMsg = \"\";\n";
				$strDatabaseClass .= "\t\t\t\t\t\$strSuccessMsg = \"\";\n";
				$strDatabaseClass .= "\t\t\tif(\$this->get_ID() == \"\")\n";
				$strDatabaseClass .= "\t\t\t\t\treturn \$this->_setFailScreens(\"Update aborted no record selected.\", true);\n";
				$strDatabaseClass .= "\n";
				$strDatabaseClass .= "\t\t\t\$strJoin = \"\";\n";
				$strDatabaseClass .= "\t\t\t\$strSql = \"UPDATE ".$qryTable->myrow[0]."\n";
				$strDatabaseClass .= "\t\t\t\t\tSET \";\n";
				/* reset row pointer */
				mysql_data_seek($qryField->result, 0);
				
				do{
					if($qryField->myrow[0] != "")
					{
						$strDatabaseClass .= "\t\t\t\t\tif(\$this->_".$qryField->myrow[0]."['updated'])\n";
						$strDatabaseClass .= "\t\t\t\t\t{\n";
						$strDatabaseClass .= "\t\t\t\t\t\t\$strSql .= \$strJoin.\" ".$qryField->myrow[0]." = '\".\$this->_".$qryField->myrow[0]."['value'].\"' \";\n";
						$strDatabaseClass .= "\t\t\t\t\t\t\$strJoin = \",\";\n";
						$strDatabaseClass .= "\t\t\t\t\t\t\$strSuccessMsg .= \"".$qryField->myrow[0]." changed from '\".\$this->_".$qryField->myrow[0]."['oldvalue'].\"' to '\".\$this->_".$qryField->myrow[0]."['value'].\"'<br/>\";\n";
						$strDatabaseClass .= "\t\t\t\t\t\t\$strErrorMsg .= \"".$qryField->myrow[0]." not updated<br/>\";\n"; 
						$strDatabaseClass .= "\t\t\t\t\t}\n";
						$strDatabaseClass .= "\n";
					}		
				}while($qryField->myrow = mysql_fetch_array($qryField->result));
				
				$strDatabaseClass .= "\t\t\t\$strSql .= \"WHERE ID = \". \$this->get_ID();\n";				
				
				$strDatabaseClass .= "\t\t\t//Only execute sql if there is something to update.\n";			
				$strDatabaseClass .= "\t\t\tif(\$strJoin == \",\")\n";	
				$strDatabaseClass .= "\t\t\t{\n";	
				$strDatabaseClass .= "\t\t\t\t//echo nl2br(\$strSql);\n";	
				$strDatabaseClass .= "\t\t\t\tif(!(\$DatabaseResult = mysql_query(\$strSql)))\n";	
				$strDatabaseClass .= "\t\t\t\t\treturn \$this->_setFailScreens(\$strErrorMsg);\n";			
				$strDatabaseClass .= "\t\t\t\t\$GLOBALS['arrSuccessMessages'][] = \$strSuccessMsg;\n";						
				$strDatabaseClass .= "\t\t\t}else\n";	 
				$strDatabaseClass .= "\t\t\t\t\$GLOBALS['arrSuccessMessages'][] = \"No data has changed, so no updates made.\";\n";							
				$strDatabaseClass .= "\t\t\treturn(TRUE);\n";	
				$strDatabaseClass .= "\t\t}\n";	
				
				
				/* Write out delete function if table has a primary key */
				if($strPrimaryKey)
					$strDatabaseClass .= createDelete($qryTable->myrow[0], $strPrimaryKey);
				
				/* Write out insert function */
				$strDatabaseClass .= createInsert($qryTable->myrow[0], $strAutoInc, $qryField);
				$strDatabaseClass .= createSearch($qryTable->myrow[0], $qryField);
				$strDatabaseClass .= "\t}\n\n";
				$strDatabaseClass .= "?>";
				/* write class to file */
				echo "<br/>".fwrite($filDbClass, $strDatabaseClass);
				fclose($filDbClass);
			}
			


		}while($qryTable->myrow = mysql_fetch_array($qryTable->result));
		//print_object($arrTable);
	?>
	</pre>
</div>	
<? 
function createDelete($strTable, $strPrimaryKey)
{
	$txtBody = <<<EOF
			public function delete_$strTable()
			{
				if(\$this->get_IDValue() == "")
					return \$this->_setFailScreens("Delete aborted no record selected.", true);
				
		
				/* Deleted port forward from agg */
				\$strSql =   "DELETE FROM $strTable
		   					  WHERE $strPrimaryKey		= ".\$this->get_IDValue();
		
				\$qryDelete$strTable = new MySqlResults(\$strSql);
		
				if(!\$qryDelete$strTable\->result)
					return (\$this->_setFailScreens("An error occurred while trying to delete database entry."));
		
		
				\$GLOBALS['arrSuccessMessages'][] = " has been removed";
				return true;
			}
EOF;
	return $txtBody."\n";
}

function createInsert($strTable, $strAutoInc, &$qryField)
{
	/* reset row pointer */
	mysql_data_seek($qryField->result, 0);
    $strClassName = str_ireplace(" ","",ucwords(str_ireplace("_"," ",$strTable)));
	$strNext = "";
	$txtBody = <<<EOF
		public function insert$strClassName()
		{
			/* Check that all required fields have been set */
			if (!(\$this->_validateAllFieldsSet()))
			{
				return (\$this->_setFailScreens("Fields required to add a new record haven't all been set", true));
			}
			
			\$strSql =   "INSERT INTO $strTable
						  (
EOF;
	
	
			do{		
				if( ($qryField->myrow[0] != "") && ($qryField->myrow[0] != $strAutoInc) )
				{
					$txtBody .= "\t\t".$strNext." 	".$qryField->myrow[0]."\n";	
					$strNext = ",";
				}		
			}while($qryField->myrow = mysql_fetch_array($qryField->result));
			$txtBody .= ")VALUE(\n";
			
			/* reset row pointer */
			mysql_data_seek($qryField->result, 0);
			$strNext = "";
			
			do{		
				if( ($qryField->myrow[0] != "") && ($qryField->myrow[0] != $strAutoInc) )
				{
					$txtBody .= "\t\t".$strNext." 	'\".\$this->get_".$qryField->myrow[0]."().\"' \n";	
					$strNext = ",";
				}		
			}while($qryField->myrow = mysql_fetch_array($qryField->result));
			$txtBody .= <<<EOF
			)";
			
			\$qryInsert$strTable = new MySqlResults(\$strSql);
		if (\$qryInsert$strTable\->result)
		{
			/*Extra post sql processing  */
		}else
			return \$this->_setFailScreens("Unable to add to  $strTable");
			
		return true;
	}
EOF;
	 
	return $txtBody."\n";
}

function createSearch($strTable, &$qryField)
{
    /* reset row pointer */
	mysql_data_seek($qryField->result, 0);
	$strNext = "";
    $strClassName = str_ireplace(" ","",ucwords(str_ireplace("_"," ",$strTable)));
    $txtBody = <<<EOF
	/**
	 * public function to search $strTable
	 * @param any that are set in \$this
	 * @return MySqlResults
	 */
	public function search$strClassName()
	{
		\$strJoin = " WHERE ";
		\$strSql = "SELECT * 
                   FROM $strTable ";\n\n
EOF;
        
		do{	
		    if($qryField->myrow[0] != "")  
			{	
                $txtBody .= "\t\tif(\$this->_get_".$qryField->myrow[0]."Value())\n";
                $txtBody .= "\t\t{\n";
			    $txtBody .= "\t\t\t\$strSql .= \$strJoin.\" ".$qryField->myrow[0]." = '\".\$this->_get_".$qryField->myrow[0]."Value().\"' \";\n";
				$txtBody .= "\t\t\t\$strJoin = \" AND \";\n";
                $txtBody .= "\t\t}\n\n";
			}
		}while($qryField->myrow = mysql_fetch_array($qryField->result));
		$txtBody .= <<<EOF

		\$qrySearch$strTable = new MySqlResults(\$strSql);
		return \$qrySearch$strTable;	
    }
EOF;

    return $txtBody."\n";
}
